package modules.majiang.viewManager
{
	import com.netease.protobuf.UInt64;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import communication.majiang.MajiangMsgEvent;
	
	import modules.GameEvent;
	import modules.ViewManager;
	import modules.majiang.MajiangEvent;
	import modules.majiang.data.TableDataModel;
	import modules.majiang.data.TablesDataManager;
	
	import utils.ResourceManager;

	/**
	 * 坐下按钮管理
	 * @author xumin.xu 2013-1-21
	 */

	public class SeatManager extends ViewManager
	{
		private var tableId:UInt64;

		/**
		 * 坐下按钮列表
		 **/
		private var seatList:Array;

		public function SeatManager(tableId:UInt64, tableView:MovieClip)
		{
			this.tableId = tableId;

			this.mainUI = tableView;

			seatList = [null, tableView.chair1Btn, tableView.chair2Btn, tableView.chair3Btn, tableView.chair4Btn, tableView.chair5Btn, tableView.chair6Btn];

			tableView.addEventListener(Event.ADDED_TO_STAGE, onAddToStage);
			tableView.addEventListener(Event.REMOVED_FROM_STAGE, removeFromStage);
		}

		public function get tableDataModel():TableDataModel
		{
			return TablesDataManager.tableDataModeDic[tableId.toString()];
		}

		private function onAddToStage(event:Event):void
		{
			mainUI.addEventListener(MouseEvent.CLICK, onClick);
			dispatcher.addEventListener(MajiangEvent.UPDATE_MAJIANG_TABLE_VIEW, onUpdateTableView);
		}

		private function removeFromStage(event:Event):void
		{
			mainUI.removeEventListener(MouseEvent.CLICK, onClick);
			dispatcher.removeEventListener(MajiangEvent.UPDATE_MAJIANG_TABLE_VIEW, onUpdateTableView);
		}

		private function onClick(event:MouseEvent):void
		{
			//获取桌子编号
			var tableId:UInt64 = tableDataModel.tableDataFromLobby.tableID;

			//最小买入
			var minBuyIn:Number = tableDataModel.tableDataFromLobby.minBuyAmount;

			//最大买入
			var maxBuyIn:Number = tableDataModel.tableDataFromLobby.maxBuyAmount;

			switch (event.target)
			{
				case mainUI.chair1Btn:
					//弹出购买赌注界面
					dispatcher.dispatchEvent(new MajiangEvent(MajiangEvent.SHOW_CASHIER_VIEW, {aim: "sitdown", chairId: 1, tableId: tableId, minBuyIn: minBuyIn, maxBuyIn: maxBuyIn}));
					break;
				case mainUI.chair2Btn:
					//弹出购买赌注界面
					dispatcher.dispatchEvent(new MajiangEvent(MajiangEvent.SHOW_CASHIER_VIEW, {aim: "sitdown", chairId: 2, tableId: tableId, minBuyIn: minBuyIn, maxBuyIn: maxBuyIn}));
					break;
				case mainUI.chair3Btn:
					//弹出购买赌注界面
					dispatcher.dispatchEvent(new MajiangEvent(MajiangEvent.SHOW_CASHIER_VIEW, {aim: "sitdown", chairId: 3, tableId: tableId, minBuyIn: minBuyIn, maxBuyIn: maxBuyIn}));
					break;
				case mainUI.chair4Btn:
					//弹出购买赌注界面
					dispatcher.dispatchEvent(new MajiangEvent(MajiangEvent.SHOW_CASHIER_VIEW, {aim: "sitdown", chairId: 4, tableId: tableId, minBuyIn: minBuyIn, maxBuyIn: maxBuyIn}));
					break;
				case mainUI.chair5Btn:
					//弹出购买赌注界面
					dispatcher.dispatchEvent(new MajiangEvent(MajiangEvent.SHOW_CASHIER_VIEW, {aim: "sitdown", chairId: 5, tableId: tableId, minBuyIn: minBuyIn, maxBuyIn: maxBuyIn}));
					break;
				case mainUI.chair6Btn:
					//弹出购买赌注界面
					dispatcher.dispatchEvent(new MajiangEvent(MajiangEvent.SHOW_CASHIER_VIEW, {aim: "sitdown", chairId: 6, tableId: tableId, minBuyIn: minBuyIn, maxBuyIn: maxBuyIn}));
					break;
				//站起
				case mainUI.standUpBtn:
					dispatcher.dispatchEvent(new MajiangEvent(MSProto.MSID_STANDUP_REQ.toString(), {tableId: tableId}));
					break;
				case mainUI.exitBtn:
					onClickBackHandler();
					break;
				case mainUI.buyInBtn:
					onClickBuyInButtonHandler();
					break;
			}
		}

		private function onUpdateTableView(event:MajiangEvent):void
		{
			if (tableId.toString() != event.data.tableId.toString())
				return;
			
			for (var i:int = 1; i <= 6; i++)
			{
				//显示座位位置
				var showChairId:int = tableDataModel.getPositionByChairId(i);

				seatList[showChairId].visible = (tableDataModel.seatedPlayers[i] == null && tableDataModel.sitChairId == -1 && tableDataModel.isTournament == false);
			}

			mainUI.standUpBtn.visible = tableDataModel.sitChairId != -1;
			
			updateBuyInBtnState();
		}
		
		/**
		 * 点击买入按钮
		 **/
		public function onClickBuyInButtonHandler():void
		{
			var tableId:UInt64 = tableDataModel.tableDataFromLobby.tableID;
			
			var minBuyIn:Number = Math.max(0, tableDataModel.tableDataFromLobby.minBuyAmount - tableDataModel.money);
			
			var maxBuyIn:Number = Math.max(0, tableDataModel.tableDataFromLobby.maxBuyAmount - tableDataModel.money);
			
			if (maxBuyIn > 0)
			{
				//弹出购买赌注界面
				dispatcher.dispatchEvent(new MajiangMsgEvent(MajiangEvent.SHOW_CASHIER_VIEW, {tableId: tableDataModel.tableDataFromLobby.tableID, minBuyIn: minBuyIn, maxBuyIn: maxBuyIn}));
			}
			else
			{
				var showInfo:String = ResourceManager.getInstance().getString('MahjongResource', 'overMaxValue');
				dispatcher.dispatchEvent(new GameEvent(GameEvent.SHOW_WARN_INFO, showInfo));
			}
		}
		
		/**
		 * 点击退出桌子按钮
		 **/
		public function onClickBackHandler():void
		{
			var data:Object = {};
			data.infoText = ResourceManager.getInstance().getString('MahjongResource', 'exitMessage');
			data.clickYesFunction = function():void
			{
				dispatcher.dispatchEvent(new MajiangMsgEvent(MSProto.MSID_LEAVE_TABLE_REQ.toString(), {tableId: tableId}));
			};
			//弹出确认界面
			dispatcher.dispatchEvent(new GameEvent(GameEvent.SHOW_INFORMATION, data));
		}
		
		public function updateBuyInBtnState():void
		{
			mainUI.buyInBtn.visible = tableDataModel.gameUIState.buyInBtnVisible;
		}
		
	}
}
